//
//  ArtGalleryVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/18.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "ArtGalleryVC.h"
#import "ArtGalleryCell_first.h"
#import "ArtGalleryCell_second.h"
#import "ArtGalleryReusableView.h"
#import "ArtGalleryCell_third.h"
#import "ArtGalleryCollectionCell.h"
#import "IGArtGalleryDetailApi.h"
#import "IGArtGalleryAttentApi.h"
#import "IGMePubTopicVC.h"
#import "ArtGalleryReusableView_second.h"
#import "ArtGalleryDetailVC.h"
@interface ArtGalleryVC ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource,UICollectionViewDelegate>
{
    BOOL isShowMore;//是否显示更多
    NSInteger heightCell;
    NSString *museumContent;//博物馆介绍
    NSString *museumImage;//博物馆图片
    NSString *museumIfAttention;
    NSString *museumTitle;//博物馆名称
    CGFloat strHeight;
}
@property (nonatomic, strong) IGArtGalleryDetailApi *api;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) IGArtGalleryAttentApi *attentApi;
@end

@implementation ArtGalleryVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isNavgationBarHidden = YES;
    
    isShowMore = NO;
    heightCell = 0;
    strHeight = 0;
    self.api = [IGArtGalleryDetailApi new];
    self.attentApi = [[IGArtGalleryAttentApi alloc] init];
    self.dataSource = [@[] mutableCopy];
    [self creatCollectionView];
    AdjustsScrollViewInsetNever(self, self.collectionView);
    
    [self dataRequest];
}

- (void)dataRequest
{
    _api.museumId = _aId;
    @KWeakObj(self)
    [self.api startWithDecodeClass:[IGArtGalleryDetailResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.collectionView ig_endHeaderRefreshing];
        if (responseObject)
        {
            IGArtGalleryDetailResp *resp = responseObject;
            IGArtGalleryDetailModel *model = resp.data;

            museumImage = model.museumImage;
            museumContent = model.museumContent;
            museumIfAttention = model.museumIfAttention;
            museumTitle = model.museumTitle;
            if (museumContent)
            {
                strHeight = [Tool getLabelSizeWithText:museumContent AndWidth:KSCREENWIDTH - 30 AndFont:[UIFont font14] attribute:nil].height;
            }
            _dataSource = [NSMutableArray arrayWithArray:model.museumProductList];
            [selfWeak.collectionView reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];
}

- (void)attentDataRequest:(NSString *)operatorType
{
    self.attentApi.operatorType = operatorType;
    self.attentApi.museumId = self.aId;
    @KWeakObj(self)
    [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            museumIfAttention = ([museumIfAttention isEqualToString: @"0"]) ? @"1" : @"0";
            [selfWeak.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:1]]];
            if ([operatorType isEqualToString:@"1"])
            {
//                [selfWeak presentMessageTips:@"取消成功"];
//                [selfWeak presentMessageTips:NSLocalizedString(@"Canceled Successfully", nil)];
            }
            else
            {
//                [selfWeak presentMessageTips:@"关注成功"];
//                [selfWeak presentMessageTips:NSLocalizedString(@"Follow Successfully", nil)];
            }
        }
//        else
//        {
//            [self presentMessageTips:@"操作失败"];
//        }
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(heightOfCell:) name:@"collectionViewHeight" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)heightOfCell:(NSNotification *)notification
{
    heightCell = [[notification.userInfo objectForKey:@"height"] integerValue];
    [_collectionView reloadData];
}

- (void)creatCollectionView
{       
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT                                                                                                                                                ) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    self.collectionView.backgroundColor = RGB(234, 234, 234);
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView ig_setHeaderWithRefreshingBlock:^{
        [self dataRequest];
    }];
    [self.collectionView registerClass:[ArtGalleryCell_first class] forCellWithReuseIdentifier:@"ArtGalleryCell_first"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ArtGalleryCell_second" bundle:nil] forCellWithReuseIdentifier:@"ArtGalleryCell_second"];
    [self.collectionView registerClass:[ArtGalleryReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ArtGalleryReusableView"];
    [self.collectionView registerClass:[ArtGalleryCollectionCell class] forCellWithReuseIdentifier:@"ArtGalleryCollectionCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ArtGalleryReusableView_second" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ArtGalleryReusableView_second"];
    [self.view addSubview:self.collectionView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return museumImage ? 3 : 0;
//    self.dataSource.count ? 3 : (museumImage ? 2 : 1);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    @KWeakObj(self)
    if (indexPath.section == 0)
    {
        ArtGalleryCell_first *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtGalleryCell_first" forIndexPath:indexPath];
        cell.imageUrl = museumImage;
        cell.cid = _aId;
        [cell setNeedsLayout];
        return cell;
    }
    else if (indexPath.section == 1)
    {
        ArtGalleryCell_second *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtGalleryCell_second" forIndexPath:indexPath];
        cell.titleL.text = museumTitle;
        cell.concernBtn.selected = [museumIfAttention isEqualToString:@"0"] ? YES : NO;
        cell.concernBlock = ^(UIButton *btn) {
            [selfWeak attentDataRequest:[NSString stringWithFormat:@"%@", btn.selected ? @"1" : @"0"]];
        };
        cell.textL.text = museumContent;
        [cell setNeedsLayout];
        return cell;
    }
    else if (indexPath.section == 2)
    {
        if (_dataSource.count > 0)
        {
            ArtGalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtGalleryCollectionCell" forIndexPath:indexPath];
            if (cell.dataSource.count !=  _dataSource.count && _dataSource.count > 0)
            {
                cell.dataSource = [NSMutableArray arrayWithArray:_dataSource];
            }
            return cell;
        }
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        IGMePubTopicVC *vc = [IGMePubTopicVC new];
        vc.pubUid = _aId;
        vc.pubTitle = museumTitle;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return CGSizeMake(KSCREENWIDTH, KSCREENWIDTH * 9 / 16);
    }
    else if (indexPath.section == 1)
    {
        if (isShowMore)
        {
            return CGSizeMake(KSCREENWIDTH, strHeight + 61);
        }
        return strHeight >= 70 ? CGSizeMake(KSCREENWIDTH, 131) :  CGSizeMake(KSCREENWIDTH, strHeight + 61);
    }
    else if (indexPath.section == 2  && _dataSource.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, (heightCell == 0) ? 30 : heightCell);
    }
    return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && strHeight >= 70)
    {
        ArtGalleryReusableView *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ArtGalleryReusableView" forIndexPath:indexPath];
        @KWeakObj(self)
        reusableV.block = ^(UIButton *btn) {
            isShowMore = btn.selected;
            [selfWeak.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:1]]];
        };
        return reusableV;
    }
    else if (indexPath.section == 2)
    {
        ArtGalleryReusableView_second *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ArtGalleryReusableView_second" forIndexPath:indexPath];
        @KWeakObj(self)
        reusableV.block = ^{
            ArtGalleryDetailVC *vc = [[ArtGalleryDetailVC alloc] init];
            vc.title = museumTitle;
            vc.aId = selfWeak.aId;
            [UIViewController ig_pushViewController:vc animated:YES];
        };
        return reusableV;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (section == 1)
    {
        return (strHeight < 70) ? CGSizeZero : CGSizeMake(KSCREENWIDTH, 20);
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 2)
    {
        return CGSizeMake(KSCREENWIDTH, 48);
    }
    return CGSizeZero;
}


@end
